package com.ficv.tucanalmovil;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

public class AvisoActivity extends Activity {

    private static final String FILENAME = "avisos.txt";

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        onNewIntent(getIntent());
		
		
        

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_aviso, menu);
        return true;
    }
    

    Empresa e=null;
    
    @Override
    public void onNewIntent(Intent intent){
    	
    	
//		String ns = Context.NOTIFICATION_SERVICE;
//		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//		mNotificationManager.cancel(101);
//
    	if(e==null) {
    		e=new Empresa();
    		e.setImgUrl("japon.jpg");
    	
    	 try {
 	        FileInputStream fis = openFileInput(FILENAME);
 	        DataInputStream in = new DataInputStream(fis);
 	        BufferedReader br = new BufferedReader(new InputStreamReader(in));
 	        String strLine;
 	        //Read File Line By Line
 	        while ((strLine = br.readLine()) != null) {
 	        	e.getAvisos().add(strLine);
 	        }
 	        //Close the input stream
 	        in.close();
         }catch (Throwable e){//Catch exception if any
 			Log.e("ERROR", e.getCause() + ": " + e.getMessage());

         }
    	 
    	}
    	
        Log.i("AVISOS", e.getAvisos().toArray().toString());
    	Bundle extras = intent.getExtras();
        if(extras != null){ 
            if(extras.containsKey("NotificationMessage"))
            {
                e.getAvisos().add(extras.getString("NotificationMessage"));
                extras.remove("NotificationMessage");
            }
        }
        
        
        FileOutputStream fos;
		try {
			fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    		for(String s:e.getAvisos()) {
    			fos.write(s.getBytes());
    			fos.write(System.getProperty("line.separator").getBytes());
    		}
	        fos.close();
		} catch (Throwable e) {
			Log.e("ERROR", e.getCause() + ": " + e.getMessage());

		}
        
        WebView myWebView = new WebView(this);
        
        myWebView.addJavascriptInterface(new JavaScriptInterface(this,myWebView,e ), "Android");
        myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 

        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        

        myWebView.loadUrl("file:///android_asset/aviso.html");

               
        setContentView(myWebView);
    }

}
